//
//  SGGlobal.swift
//  SGSwiftExtensionDemo
//
//  Created by xyanl on 2025/5/20.
//

import Foundation
import UIKit

// MARK: - 屏幕尺寸常用的常量
/// 屏幕的宽
let sgScreenW = UIScreen.main.bounds.width
/// 屏幕的高
let sgScreenH = UIScreen.main.bounds.height

/// 顶部安全高度
let sgIphoneTopSafeHeight = UIDevice.topSafeHeight
/// 底部安全区域高度
let sgIphoneBottomSafeHeight = UIDevice.bottomSafeHeight
/// 顶部状态栏高度（包括安全区）
let sgIphoneStatusBarHeight = UIDevice.statusBarHeight
/// 导航栏高度
let sgIphoneNavBarHeight = UIDevice.navBarHeight
/// 状态栏+导航栏的高度
let sgIphoneNavStatusBarHeight = UIDevice.navBarFullHeight
/// 底部导航栏高度
let sgIphoneTabBarHeight = UIDevice.tabBarHeight
/// 底部导航栏高度（包括安全区）
let sgIphoneTabBarSafeHeight = UIDevice.tabBarFullHeight


/// 获取当前控制器
var sgCurrentController: UIViewController? {
    UIViewController.sg.currentViewController
}

var sgKeyWindow: UIWindow {
    UIWindow.sg.keyWindow
}

// 获取主 window view
var sgKeyWindowView: UIView {
    var window = sgKeyWindow
    if window.windowLevel != UIWindow.Level.normal {
        if let windowArray = UIApplication.shared.connectedScenes
            .map({ $0 as? UIWindowScene })
            .compactMap({ $0 })
            .first?.windows {
            for tempWin in windowArray {
                if tempWin.windowLevel == UIWindow.Level.normal {
                    window = tempWin
                    break
                }
            }
        }
    }
    return window
}


// MARK: - print
func sgLog<T>(_ message: T, file: String = #file, function: String = #function, lineNumber: Int = #line) {
#if DEBUG
    // 1.获取文件名,包含后缀名
    let name = (file as NSString).lastPathComponent
    // 1.1 切割文件名和后缀名
    let fileArray = name.split(separator: ".")
    // 1.2 获取文件名
    let fileName = fileArray[0]
    let time = Date.sg.timeString(formatter: "HH:mm:ss sss")
    print("\(String(describing: time)) \(fileName) [line:\(lineNumber)] \(message)")
#endif
}
